package com.whmnx.login.ui.model;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.whmnx.base.bean.UserBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.CodeTimeUtils;
import com.whmnx.base.utils.UItils;
import com.whmnx.login.databinding.FragmentForgetBinding;
import com.whmnx.login.ui.ForgetPassWordFragment;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * author : huang
 * date   : 2020/10/9 0009 11:36
 * version: 1.0
 * @author Administrator
 */
public class ForgetModel extends BaseViewModel {

    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //密码的绑定
    public ObservableField<String> password = new ObservableField<>("");


    public ObservableField<String> code = new ObservableField<>("");
    public ObservableField<String> renPayPas = new ObservableField<>("");

    private ForgetPassWordFragment mContext;

    FragmentForgetBinding binding;


    public ForgetModel(@NonNull Application application) {
        super(application);
    }

    public void initData(ForgetPassWordFragment activity, FragmentForgetBinding binding) {
        this.mContext = activity;
        this.binding = binding;
    }


    //验证码的点击事件
    public BindingCommand codeOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getCode();
        }
    });

    //登录按钮的点击事件
    public BindingCommand loginOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            login();
        }
    });
    //登录按钮的点击事件
    public BindingCommand wangJiPasOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
         finish();
        }
    });

    //忘记密码 确认
    private void login() {
        if (TextUtils.isEmpty(code.get())) {
            ToastUtils.showShort("请输入验证码！");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort("请输入新密码！");
            return;
        }

        if (TextUtils.isEmpty(renPayPas.get())) {
            ToastUtils.showShort("请输入确认密码！");
            return;
        }
        if (!password.get().equals(renPayPas.get())){
            ToastUtils.showShort("2次输入密码不一致！");
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("phone",userName.get());
        params.put("code",code.get());
        params.put("pwd",UItils.md5Digest(password.get()));
        params.put("type","0");
        HttpClient.get(mContext.getActivity(),Constants.RETRIEVEPWD, params,new CallBack<UserBean>() {
            @Override
            public void onSuccess(UserBean result) {
               finish();
            }
        });
    }


    private void getCode() {
        if (!UItils.isPhoneNumber(userName.get())){
            ToastUtils.showShort("请输入手机号！");
            return;
        }

        Map<String, Object> params = new HashMap<>();
        params.put("Mobile",userName.get());
        HttpClient.get(mContext.getActivity(), Constants.GETCODE, params,new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
                CodeTimeUtils.countDowns(binding.etCode);
            }
        });
    }
}
